package com.example.hotelmanagement.service.impl;

import com.example.hotelmanagement.common.pojo.PageResult;
import com.example.hotelmanagement.domain.entity.Guest;
import com.example.hotelmanagement.domain.vo.GuestPageReqVo;
import com.example.hotelmanagement.domain.vo.GuestReqVo;
import com.example.hotelmanagement.mapper.GuestMapper;
import com.example.hotelmanagement.service.GuestService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 客户表(Guest)表服务实现类
 *
 * @author makejava
 * @since 2023-06-10 17:32:50
 */
@Service
public class GuestServiceImpl implements GuestService {
    @Resource
    private GuestMapper guestMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Guest queryById(Long id) {
        return guestMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param guest 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResult<Guest> queryByPage(GuestPageReqVo guest) {
        return guestMapper.selectPageByCondition(guest);
    }

    /**
     * 新增数据
     *
     * @return 实例对象
     */
    @Override
    public Guest insert(GuestReqVo guestReqVo) {
        Guest guest = new Guest();
        BeanUtils.copyProperties(guestReqVo,guest);
        guest.setType(1);
        //id由数据库生成，此处过滤
        guest.setId(null);
        guest.setCreateTime(new Date());
        guestMapper.insert(guest);
        return guest;
    }

    /**
     * 修改数据
     *
     * @param guest 实例对象
     * @return 实例对象
     */
    @Override
    public Guest update(Guest guest) {
        guest.setCreateTime(null);
        guestMapper.updateById(guest);
        return queryById(guest.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return guestMapper.deleteById(id) > 0;
    }
}
